<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>权限管理系统</title>
    <jsp:include page="/views/include.jsp"></jsp:include>
    <script type="text/javascript">
    	var contextPath = '<%=request.getContextPath()%>';
    	$.parser.onComplete = function(){
        	$('body').css('visibility','visible');
        	setTimeout(function(){
         	$('#loading-mask').remove();
        	},50);
    	};
    	$(function(){
        	$(window).resize(function(){
            	$('#mainlayout').layout('resize');
        	});
    	});
    </script>
	<script type="text/javascript">
	//----------------------------------------------用来格式化显示状态，是否打开和是否叶子
	var status1;
	$.getJSON("<c:url value='/permissions/outDicJsonByNicknameActions.tg?nickName=status'/>", function(json){
		status1=json;
	});
	function statusFormatter(value){
		for(var i=0; i<status1.length; i++){		
			if (status1[i].value == value) return status1[i].name;
		}
		return value;
	}
	var editStatus;
	$.getJSON("<c:url value='/permissions/outDicJsonByNicknameActions.tg?nickName=editstatus'/>", function(json){
		editStatus=json;
	});
	function editStsFormatter(value){
		for(var i=0; i<editStatus.length; i++){		
			if (editStatus[i].value == value) return editStatus[i].name;
		}
		return value;
	}
	
	var sex;
	$.getJSON("<c:url value='/permissions/outDicJsonByNicknameActions.tg?nickName=sex'/>", function(json){
		sex=json;
	});
	function sexFormatter(value){
		for(var i=0; i<sex.length; i++){
			if (sex[i].value == value) return sex[i].name;
		}
		return value;
	}
	var usertype;
	$.getJSON("<c:url value='/permissions/outDicJsonByNicknameActions.tg?nickName=usertype'/>", function(json){
		usertype=json;
	});
	function usertypeFormatter(value){
		for(var i=0; i<usertype.length; i++){
			if (usertype[i].value == value) return usertype[i].name;
		}
		return value;
	}
	//---------------------------------------------------------------------
	
		var url;	
	
		$(function(){
			$('#tree').tree({
				onClick : function(node) {
					var id =node.id.substr(4);
					if('ual' == id){
						url = '<c:url value='/permissions/getGroupAllPageUser.tg'/>';
					}else{
						url = '<c:url value='/permissions/getGroupAllPageUser.tg'/>?id=' + id;
					}
					$('#dt-resources').datagrid('options').url = url;
					$('#dt-resources').datagrid("reload");
					
					if('ual' == id){
						return;
					}else{
						url = '<c:url value="/permissions/findGroup.tg"/>?id=' + id;
					}
					$('#myform').form('submit', {
						url:url,
						onSubmit: function(){
							return true;
						},
						success: function(data){
							data=eval('('+data+')');
							$('#lname').val(data.rows[0].name);
							$('#lenname').val(data.rows[0].enName);
							$('#lgrouptype').val(data.rows[0].groupType);
							$('#lorderid').val(data.rows[0].orderId);
							$('#lmemo').val(data.rows[0].memo);
							$('#lstatus').val(data.rows[0].status);
							$('#leditStatus').val(data.rows[0].editStatus);							
						}
					});
				}
			});
		});
		
		function newItem(){
			$('#myform').form('clear');
			var obj = $('#tree').tree('getSelected');
			if(obj){
				var id =obj.id.substr(4);
				if('ual' == id){
					$('#parentId').combotree({
						url:'<c:url value="/permissions/getTreeGroup.tg"/>',
						valueField:'id',
						textField:'text'
					});
					url = '<c:url value="/permissions/saveGroup.tg"/>';
					$('#dlg').dialog('setTitle', '填写组资料').dialog('open');
					return;
				}else{
					url = '<c:url value="/permissions/findGroup.tg"/>?id=' + id;
				}
				$('#myform').form('submit', {
					url:url,
					onSubmit: function(){
						return true;
					},
					success: function(data){
						data=eval('('+data+')');
						var tempObj = data.rows[0];
						$('#parentId').combotree({
							url:'<c:url value="/permissions/getTreeGroup.tg"/>',
							valueField:'id',
							textField:'text'
						});
						$('#parentId').combotree('setText',data.parentName);
						$('#parentId').combotree('setValue',tempObj.id);
					}
				});
			}else{
				$('#parentId').combotree({
					url:'<c:url value="/permissions/getTreeGroup.tg"/>',
					valueField:'id',
					textField:'text'
				});
			}
			$('#status').combobox('setValue','1');
			$('#name').removeAttr("readonly");
			url = '<c:url value="/permissions/saveGroup.tg"/>';
			$('#dlg').dialog('setTitle', '填写组资料').dialog('open');
			
		}
		
		function loadAllTree(){
			url = '<c:url value="/permissions/indexGroup.tg"/>';
			$('groupForm').form('submit',{
				url:url,
				onSubmit: function(){
					return $('#myform').form('validate');
				},
				success: function(data){
					$("#allTrees").html('');
					$("#allTrees").append(data);
					$("#tree").tree();
				}
			});
		}
		
		function saveItem(){
			$('#myform').form('submit', {
				url:url,
				onSubmit: function(){
					return $('#myform').form('validate');
				},
				success: function(data){
					$('#dlg').dialog('close');
					data=eval('('+data+')');
					if(data.success){
						$.messager.show(
							{
								title:'提示',
								msg:'操作成功！',
								showType:'slide'
							}
						);
						loadAllTrees();
					}
					if(data.error){
						$.messager.alert('提示','操作失败！','error');
					}
				}
			});
		}
		
		//==========================================
		function loadAllTrees(){
			url = '<c:url value="/permissions/listGroup.tg"/>?';
			 $('#myform').form('submit',{
					url:url,
					onSubmit:function(){return true;},
					success:function(data){
						$("#allTrees").html("");
						$("#allTrees").append($(data));
						$("#tree").tree();
						$('#tree').tree({
							onClick : function(node) {
								var id =node.id.substr(4);
								if('ual' == id){
									url = '<c:url value="/permissions/getGroupAllPageUser.tg"/>';
								}else{
									url = '<c:url value="/permissions/getGroupAllPageUser.tg"/>?id=' + id;
								}
								$('#dt-resources').datagrid('options').url = url;
								$('#dt-resources').datagrid("reload");
							}
						});
						url = "<c:url value='/permissions/getGroupAllPageUser.tg'/>";
						$('#dt-resources').datagrid('options').url = url;
						$('#dt-resources').datagrid("reload");
					}
				});
		}
		//==========================================
		
		
		
		function delItem(){
			var obj = $('#tree').tree('getSelected');
			if(obj){
				var id =obj.id.substr(4);
				if('ual' == id||'1'==id){
					$.messager.alert('提示','根元素和内置用户组不能删除','info');
					return;
				}else{
					url = '<c:url value="/permissions/delGroup.tg"/>?id=' + id;
				}
				if(confirm('确定要删除？')){
					$('#myform').form('submit', {
						url:url,
						onSubmit: function(){
							return true;
						},
						success: function(data){
							data=eval('('+data+')');
							if(data.success){
								$.messager.show(
									{
										title:'提示',
										msg:'操作成功！',
										showType:'slide'
									}
								);
								loadAllTrees();
							}
							if(data.error){
								$.messager.alert('提示','操作失败！'+data.message,'error');
							}
						}
					});
				}
			}else{
				$.messager.alert('提示','请选择要删除的组','info');
			}
		}
		function editItem(){
			$('#myform').form('clear');
			var obj = $('#tree').tree('getSelected');
			if(obj){
				var id =obj.id.substr(4);
				if('ual' == id||'1'==id){
					$.messager.alert('提示','根元素和内置用户组不能修改','info');
					return;
				}else{
					url = '<c:url value="/permissions/findGroup.tg"/>?id=' + id;
				}
				
				$('#myform').form('submit', {
					url:url,
					onSubmit: function(){
						return true;
					},
					success: function(data){
						data=eval('('+data+')');
						$('#name').val(data.rows[0].name);
						$('#enName').val(data.rows[0].enName);
						$('#groupType').val(data.rows[0].groupType);
						$('#orderId').val(data.rows[0].orderId);
						$('#memo').val(data.rows[0].memo);
						$('#myhomeid').val(data.rows[0].id);
						
						$('#parentId').combotree({
							url:'<c:url value="/permissions/getTreeGroup.tg"/>',
							valueField:'id',
							textField:'text'
						});
						
						
						$('#parentId').combotree('setValue',data.parentid);
						$('#parentId').combotree('setText',data.parentName);
						
						$('#status').combobox('setValue',data.rows[0].status);
						$('#name').attr("readonly","true");
					}
				});

				$('#dlg').dialog('setTitle', '修改组资料').dialog('open');
				url = '<c:url value="/permissions/updateGroup.tg"/>';
			}else{
				$.messager.alert('提示','请选择要修改的组','info');
			}
		}
		function lookItem(){
			var obj = $('#tree').tree('getSelected');
			if(obj){
				var id =obj.id.substr(4);
				if('ual' == id){
					$.messager.alert('提示','根元素不允许查看','info');
					return;
				}else{
					url = '<c:url value="/permissions/findGroup.tg"/>?id=' + id;
				}
				$('#myform').form('submit', {
					url:url,
					onSubmit: function(){
						return true;
					},
					success: function(data){
						data=eval('('+data+')');
						$('#lname').val(data.rows[0].name);
						$('#lenname').val(data.rows[0].enName);
						$('#lgrouptype').val(data.rows[0].groupType);
						$('#lorderid').val(data.rows[0].orderId);
						$('#lmemo').val(data.rows[0].memo);	
						$('#leditStatus').val(data.rows[0].editStatus);
						if('1' == data.rows[0].status){
							$('#lstatus').val('启用');
						}else{
							$('#lstatus').val('禁用');
						}
						if('E0M' == data.rows[0].editStatus){
							$('#leditStatusName').val('编辑');
						}else if('E0A' == data.rows[0].editStatus){
							$('#leditStatusName').val('审批中');
						}else if('E0S' == data.rows[0].editStatus){
							$('#leditStatusName').val('同步');
						}else if('E0P' == data.rows[0].editStatus){
							$('#leditStatusName').val('审批通过');
						}else if('E0F' == data.rows[0].editStatus){
							$('#leditStatusName').val('审批不通过');
						}
					}
				});
				$('#look').dialog('setTitle', '查看组资料').dialog('open');
			}else{
				$.messager.alert('提示','请选择要查看的组','info');
			}
		}
		function addUserItem(){
			
			var obj = $('#tree').tree('getSelected');
			if(obj){
				var id =obj.id.substr(4);
				if('ual' == id){
					$.messager.alert('提示','根元素不允许添加','info');
					return;
				}else{
					url = '<c:url value="/permissions/findGroup.tg"/>';
					$('#id').val(id);
				}
				$('#groupForm').form('submit', {
					url:url,
					onSubmit: function(){
						return true;
					},
					success: function(data){
						data=eval('('+data+')');
						$('#adduser').dialog('setTitle', '添加用户到' + data.rows[0].name + '组').dialog('open');
					}
				});
				$('#qcompanyId').combotree({
					url:'<c:url value="/permissions/getTreeCompany.tg"/>',
					valueField:'id',
					textField:'text',
					onLoadSuccess: function (row, data) { 
						 $('#qdeptId').combotree('tree').tree("collapseAll");
					}
				});
				
				url = '<c:url value="/permissions/getGroupOtherPageUser.tg"/>?id='+id;				
				$('#other-users').datagrid('options').url = url;
				$('#other-users').datagrid("reload");
				$('#groupid').val(id);
			}else{
				$.messager.alert('提示','请先选择组','info');
			}
		}
		function searchAddUsers()
		{
			var obj = $('#tree').tree('getSelected');
			var id =obj.id.substr(4);			

			var param = {};
			param['id'] = id;
			param['user.logonid'] = $('#qloginid').val();
			param['user.name'] = $('#quserName').val();
			param['user.companyId'] = $('#qcompanyId').combotree('getValue');
			param['user.status'] = $('#qstatus').combobox('getValue');	
			
			$('#other-users').datagrid("load",param);
		}
		function addUsers(){
			url = '<c:url value="/permissions/addUserToGroup.tg"/>'; 
			var rows = $('#other-users').datagrid('getSelections');
			if(rows){
				var ids = "";
				for(var i=0; i<rows.length; i++){
					if(i!=0)
					{
						ids = ids + ','+rows[i].id;
					}
					else
					{
						ids = rows[i].id;
					}					
				}
				if(ids.length==0)
				{
					alert('请选择要增加的用户！');
					return;
				}
				$('#userIds').val(ids);
				$('#groupForm').form('submit', {
					url:url,
					onSubmit: function(){
						return true;
					},
					success: function(data){
						data=eval('('+data+')');						
						$('#adduser').dialog('close');
						url = '<c:url value="/permissions/getGroupAllPageUser.tg"/>?id=' + $('#groupid').val();
						$('#dt-resources').datagrid('options').url = url;
						$('#dt-resources').datagrid("reload");
					}
				});
			}else{
				$.messager.alert('提示','请先选择要增加的用户','info');
			}
			
		}
		function selectAll(tem){
			if(tem.checked){
				$('#adduser form table tbody').find('input').each(function(i){
					$(this).attr('checked',true);
				});
			}else{
				$('#adduser form table tbody').find('input').each(function(i){
					$(this).removeAttr('checked');
				});
			}
		}
		
		function delUserItem(){
			var obj = $('#tree').tree('getSelected');
			if(obj){
				var id =obj.id.substr(4);
				if('ual' == id){
					$.messager.alert('提示','根元素内成员不能删除','info'); 
					return;
				}

				var row = $('#dt-resources').datagrid('getSelections');
				if(row.length != 0){
					$('#groupid').val(id);
					if(confirm('确实要删除这些用户吗？')){
						var temp = '';
						for(var i=0;i<row.length;i++){
							temp = temp + row[i].id + ',';
						}
						$('#id').val(temp);
						url = '<c:url value="/permissions/delUserFromGroup.tg"/>'; 
						$('#groupForm').form('submit', {
							url:url,
							onSubmit: function(){
								return true;
							},
							success: function(data){
								url = '<c:url value="/permissions/getGroupAllPageUser.tg"/>?id=' + $('#groupid').val();
								$('#dt-resources').datagrid('options').url = url;
								$('#dt-resources').datagrid("reload");
								data=eval('('+data+')');
								if(data.success){
									$.messager.show(
											{
												title:'提示',
												msg:'操作成功！',
												showType:'slide'
											}
									);
								}
								if(data.error){
									$.messager.alert('提示','操作失败！','error');
								}
							}
						});
					}
				}else{
					$.messager.alert('提示','请选择组内成员','info');
				}
			}else{
				$.messager.alert('提示','请选择组','info');
			}
		}
		function delAllUserItem(){
			var obj = $('#tree').tree('getSelected');
			if(obj){
				var id =obj.id.substr(4);
				if('ual' == id){
					$.messager.alert('提示','根元素内成员不能删除','info'); 
					return;
				}

				if(confirm('确实要删除改组所有的用户吗？')){
					$('#groupid').val(id);
					$('#id').val('ALLGROUPUSER');
					url = '<c:url value="/permissions/delUserFromGroup.tg"/>'; 
					$('#groupForm').form('submit', {
						url:url,
						onSubmit: function(){
							return true;
						},
						success: function(data){
							url = '<c:url value="/permissions/getGroupAllPageUser.tg"/>?id=' + $('#groupid').val();
							$('#dt-resources').datagrid('options').url = url;
							$('#dt-resources').datagrid("reload");
							data=eval('('+data+')');
							if(data.success){
								$.messager.show(
										{
											title:'提示',
											msg:'操作成功！',
											showType:'slide'
										}
								);
							}
							if(data.error){
								$.messager.alert('提示','操作失败！','error');
							}
						}
					});					
				}				
			}else{
				$.messager.alert('提示','请选择组','info');
			}
		}		
		function groupToRole(){
			$('#rolGroup form table tbody').html('');
			
			var obj = $('#tree').tree('getSelected');
			if(obj){
				var id =obj.id.substr(4);
				if('ual' == id){
					$.messager.alert('提示','根元素内成员不授权','info'); 
					return;
				}

				$('#rolGroup').dialog('setTitle', '组授权角色').dialog('open');
				
				$('#addRoleBt').show();
				$('#removeRoleBt').hide();
				
				$('#groupid').val(id);
				url = '<c:url value="/permissions/otherRoleFromGroup.tg"/>';  
				$('#groupForm').form('submit', {
					url:url,
					success: function(data){
						$('#gsid').val(id);
						data=eval('('+data+')');
						for(var i=0;i<data.length;i++){
							var temp = '<tr><td><input type="checkbox" name="roleIds" value="' + data[i].id + '"/></td><td>' + data[i].name + '</td><td>&nbsp;</td><td>' + data[i].memo + '</td></tr>';
							$('#rolGroup form table tbody').append(temp);
						}
					}
				});
			}else{
				$.messager.alert('提示','请选择组','info');
			}
		}
		function addRole(){
			url = '<c:url value="/permissions/addRoleToGroup.tg"/>';  
			$('#selectRole').form('submit', {
				url:url,
				success: function(data){
					$('#rolGroup').dialog('close');
					$.messager.show(
							{
								title:'提示',
								msg:'操作成功！',
								showType:'slide'
							}
						);					
				}
			});
		}
		function lookGroupHaveRole(){
			var obj = $('#tree').tree('getSelected');
			if(obj){
				var id =obj.id.substr(4);
				if('ual' == id){
					$.messager.alert('提示','根元素内成员不授权','info'); 
					return;
				}
				$('#rolGroup form table tbody').html('');
				$('#addRoleBt').hide();
				$('#removeRoleBt').hide();
				$('#rolGroup').dialog('setTitle', '查看组拥有的角色').dialog('open'); 
				$('#groupid').val(id);
				url = '<c:url value="/permissions/groupHaveRole.tg"/>';  
				$('#groupForm').form('submit', {
					url:url,
					success: function(data){
						data=eval('('+data+')');
						for(var i=0;i<data.length;i++){
							var temp = '<tr><td></td><td>' + data[i].name + '</td><td>&nbsp;</td><td>' + data[i].memo + '</td></tr>';
							$('#rolGroup form table tbody').append(temp);
						}
					}
				});
			}else{
				$.messager.alert('提示','请选择组','info');
			}
		}
		function removeGroupHaveRole(){
			var obj = $('#tree').tree('getSelected');
			if(obj){
				var id =obj.id.substr(4);
				if('ual' == id){
					$.messager.alert('提示','根元素内没有角色','info'); 
					return;
				}

				$('#rolGroup form table tbody').html('');
				$('#addRoleBt').hide();
				$('#removeRoleBt').show();
				$('#rolGroup').dialog('setTitle', '移除组拥有的角色').dialog('open'); 
				$('#groupid').val(id);
				
				url = '<c:url value="/permissions/groupHaveRole.tg"/>';  
				$('#groupForm').form('submit', {
					url:url,
					success: function(data){
						$('#gsid').val(id);
						data=eval('('+data+')');
						for(var i=0;i<data.length;i++){
							var temp = '<tr><td><input type="checkbox" name="roleIds" value="' + data[i].id + '"/></td><td>' + data[i].name + '</td><td>&nbsp;</td><td>' + data[i].memo + '</td></tr>';
							$('#rolGroup form table tbody').append(temp);
						}
					}
				});
			}else{
				$.messager.alert('提示','请选择组','info');
			}
		}
		function removeRole(){
			actionUrl = '<c:url value="/permissions/removeGroupHaveRole.tg"/>'; 
			$('#selectRole').form('submit', {
				url:actionUrl,
				success: function(data){
					data=eval('('+data+')');
					$('#rolGroup').dialog('close');
					$.messager.show(
							{
								title:'提示',
								msg:'操作成功！',
								showType:'slide'
							}
						);					
				}
			});
		}
		function clickeAll(tem){
			if(tem.checked){
				$('#rolGroup form table tbody').find('input').each(function(i){
					$(this).attr('checked',true);
				});
			}else{
				$('#rolGroup form table tbody').find('input').each(function(i){
					$(this).removeAttr('checked');
				});
			}
		}
		
	</script>
